#include "client.h"

client::client(int port,string ip):server_port(port),server_ip(ip) {}
client::~client()
{
    close(client_sockfd);
}

void client::run()
{
    client_sockfd=socket(AF_INET,SOCK_STREAM,0);

    sockaddr_in server_addr;
    server_addr.sin_family=AF_INET;
    server_addr.sin_port=htons(server_port);
    server_addr.sin_addr.s_addr=inet_addr(server_ip.c_str());
    if(connect(client_sockfd,(sockaddr *)&server_addr,sizeof(server_addr)))
    {
        perror("connect err");
        exit(1);
    }

    cout<<"连接服务器成功"<<endl;
    client::HandleClient(client_sockfd);
}


void client::HandleClient(int conn)
{
    int choice;
    string name,pass,pass1;
    bool if_login=false;//记录是否登录成功
    string login_name;//记录成功登录的用户名


    cout<<"      主界面       "<<endl;
    cout<<"                  "<<endl;
    cout<<"      1,登录       "<<endl;
    cout<<"      2,注册       "<<endl;
    cout<<"      0,退出       "<<endl;  
    cout<<"______________________"<<endl;
    
    while(1)
    {
        if(if_login) break;

        cout<<"请选择操作:"<<endl;
        cin>>choice;
        if(choice==0) break;
        else if(choice==1)
        {
            string name,pass;
            cout<<"用户名：";
            cin>>name;
            cout<<"密码：";
            cin>>pass;
            string sendstr="login";
            sendstr+=name;
            sendstr+="pass:";
            sendstr+=pass;
            send(conn,sendstr.c_str(),sendstr.length(),0);
            char buf[1000];
            memset(buf,0,sizeof(buf));
            recv(conn,buf,sizeof(buf),0);
            cout<<buf<<endl;
            if(strcmp(buf,"ok")==0)
            {
                if_login=true;
                login_name=name;
                cout<<"登陆成功!"<<endl;
                break;
            }
            else
            {
                cout<<"账号或密码错误"<<endl;

            }
        }
        else if(choice==2)
        {
            cout<<"请输入用户名:";
            cin>>name;
            while(1)
            {   cout<<endl<<"请输入密码:";
                cin>>pass;
                cout<<"请再次输入密码以确认:";
                cin>>pass1;
                if(pass!=pass1)
                {
                    cout<<"2次密码不一致"<<endl;
                }
                else break;
            }
            string sendstr="name:"+name;
            sendstr+="pass:"+pass;
            send(conn,sendstr.c_str(),sendstr.length(),0);
            cout<<"注册成功"<<endl<<endl;
            cout<<"请输入您接下来的操作:"<<endl<<endl;
        }
    }
    while(if_login)
    {   
        cout<<"      主界面           "<<endl;
        cout<<"                      "<<endl;
        cout<<"      1,记账           "<<endl;
        cout<<"      2,查看花销        "<<endl;
        cout<<"      3,设置提醒额度     "<<endl;
        cout<<"      0,退出           "<<endl;
        cin>>choice;
        if(choice==1)
        {
            string buf;
            string sendstr="recordtime:";
            cout<<"请输入消费时间: ";
            cin>>buf;
            sendstr+=buf+"way:";
            cout<<"请输入消费方式(微信，银行卡，支付宝，现金): ";
            cin>>buf;
            sendstr+=buf+"pay:";
            cout<<"请输入花费金额: ";
            cin>>buf;
            sendstr+=buf+"place:";
            cout<<"请输入花费地点: ";
            cin>>buf;
            sendstr+=buf+"aim:";
            cout<<"请输入花费目的: ";
            cin>>buf;
            sendstr+=buf;
            cout<<sendstr<<endl;
            // cout<<sizeof(sendstr)<<endl;
            send(conn,sendstr.c_str(),sendstr.size(),0);
            cout<<"记录成功"<<endl;
        }
        else if(choice==2)
        {
            cout<<"       查询       "<<endl;
            cout<<"                  "<<endl;
            cout<<"     1,本日消费    "<<endl;
            cout<<"     2,本月消费    "<<endl;
            cout<<"     3,历史消费    "<<endl;
            cout<<"     0,返回上一页   "<<endl;
            cout<<endl;
            string query;
            string sendstr="query";
            cin>>query;
            if(query[0]=='0') continue;
            sendstr+=query;
            cout<<sendstr<<"(测试代码)"<<endl;
            send(conn,sendstr.c_str(),sendstr.size(),0);
            char buf[1000];
            memset(buf,0,sizeof buf);
            recv(conn,buf,sizeof(buf),0);
            // cout<<buf<<endl<<endl;
            int row=buf[0]-'0';
            if(row==0) cout<<"暂无记录!"<<endl;
            else
            {

                cout<<"一共有"<<row<<"条记录!"<<endl;
                cout<<"记录如下:"<<endl;
                cout<<"  时间\t"<<"   方式\t"<<"   金额\t"<<"目的\t"<<endl;
                while(row--)
                {
                    memset(buf,0,sizeof buf);
                    recv(conn,buf,sizeof buf,0);
                    // string res(buf);
                    // cout<<row<<":"<<endl;
                    
                    cout<<buf<<endl;
                    // cout<<res<<endl;
                    // string::size_type pos=0;
                    // int last=0;
                    // while((pos=res.find("|",pos))!=string::npos)
                    // {
                    //     string str=res.substr(last,pos-last);
                    //     cout<<str<<"\t";
                    //     last=pos;
                    //     pos++;
                    // }
                    // cout<<endl;
                }
                cout<<"_________________________"<<endl;
                memset(buf,0,sizeof buf);
                recv(conn,buf,sizeof buf,0);
                cout<<"共计:       "<<buf<<endl<<endl;
            }
        }
    }
}